package org.yiqixue.accounts.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.yiqixue.accounts.entity.Account;
import org.yiqixue.accounts.repository.AccountRepository;

/**
 * 账户控制器
 * 处理所有与账户相关的HTTP请求
 */
@RestController
@RequestMapping("/api/accounts")
@RequiredArgsConstructor
@Tag(name = "账户管理", description = "处理账户相关的所有操作")
public class AccountController {

    private final AccountRepository accountRepository;

    /**
     * 创建新账户
     * @param account 账户信息
     * @return 创建的账户信息
     */
    @PostMapping
    @Operation(summary = "创建新账户", description = "创建一个新的银行账户")
    public ResponseEntity<Account> createAccount(@RequestBody Account account) {
        return ResponseEntity.ok(accountRepository.save(account));
    }

    /**
     * 根据账号查询账户
     * @param accountNumber 账号
     * @return 账户信息
     */
    @GetMapping("/{accountNumber}")
    @Operation(summary = "查询账户", description = "根据账号查询账户信息")
    public ResponseEntity<Account> getAccount(@PathVariable String accountNumber) {
        return accountRepository.findByAccountNumber(accountNumber)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 根据手机号查询账户
     * @param phoneNumber 手机号
     * @return 账户信息
     */
    @GetMapping("/phone/{phoneNumber}")
    @Operation(summary = "根据手机号查询账户", description = "根据手机号查询账户信息")
    public ResponseEntity<Account> getAccountByPhone(@PathVariable String phoneNumber) {
        return accountRepository.findByPhoneNumber(phoneNumber)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
} 